package com.jinghong.registerlogin.service.impi;

import com.jinghong.registerlogin.mapper.UserMapper;
import com.jinghong.registerlogin.model.pojo.Result;
import com.jinghong.registerlogin.model.pojo.User;
import com.jinghong.registerlogin.service.UserService;
import com.jinghong.registerlogin.utils.JwtUtils;
import com.jinghong.registerlogin.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:UserServiceimpi
 * Package:com.jingjian.gamesetupapp.service.impi
 * Description:
 *
 * @Author: 寻月君子
 * @CreatTime:2023/12/18 22:27
 * Version:1.0
 */
@Service
public class UserServiceimpi implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }


    @Override
    public Result register(String username, String password) {
        //查询用户
        User u = userMapper.findByUserName(username);
        if (u == null) {
            //没有占用,注册

            //加密
            String md5String = Md5Util.getMD5String(password);
            //添加
            User user = new User();
            user.setUsername(username);
            user.setPassword(md5String);
            user.setIsAdmin(1);
            userMapper.add(user);
            return Result.success();
        } else {
            //占用
            return Result.error("用户名已被占用");
        }
    }


    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateIsAdmin(Integer userId, Integer isAdmin) {
        userMapper.updateIsAdmin(userId,isAdmin);
    }

    @Override
    public Result loginByUserName(String username, String password) {
        //根据用户名查询用户
        User loginUser = userMapper.findByUserName(username);
        //判断该用户是否存在
        if (loginUser == null) {
            return Result.error("用户名错误");
        }

        //判断密码是否正确
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            //登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username",loginUser.getUsername());
            claims.put("isAdmin",loginUser.getIsAdmin());

            String token = JwtUtils.generateJwt(claims);
            return Result.success(token);
        }

        //登录失败, 返回错误信息
        return Result.error("密码错误");
    }

    @Override
    public Result updatePassword(String username, String oldPassword, String password) {
        User user = userMapper.findByUserName(username);
        String md5oldpw = Md5Util.getMD5String(oldPassword);
        if (user.getPassword() .equals(md5oldpw)){
            //对新密码加密
            String Md5password = Md5Util.getMD5String(password);
            user.setPassword(Md5password);
            userMapper.update(user);
            return Result.success();
        }else {
            return Result.error("原密码错误");
        }
    }

}
